﻿using System;
using System.Windows;
using System.Windows.Controls;
using CPN.PJN.Silverlight.Common.ExceptionHandling;

namespace CPN.PJN.Silverlight.Core
{
    /// <summary>
    /// Cette classe représente l'application CPN.PJN
    /// </summary>
    public partial class App : Application
    {
        #region Constructor

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        #endregion

        #region Application_Startup Method

        /// <summary>
        /// Cette méthode permet de gérer le démarage de l'application
        /// </summary>
        /// <param name="sender">Application</param>
        /// <param name="e">Argument de démarrage</param>
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }

        #endregion

        #region Application_Exit Method

        /// <summary>
        /// Cette méthode permet de gérer la fermeture de l'application
        /// </summary>
        /// <param name="sender">Application</param>
        /// <param name="e">Argument</param>
        private void Application_Exit(object sender, EventArgs e)
        {

        } 

        #endregion

        #region Application_UnhandledException Method
        
        /// <summary>
        /// Cette méthode permet de gérer une exeption applicative
        /// </summary>
        /// <param name="sender">Application</param>
        /// <param name="e">Argument contenant l'exception</param>
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // a ChildWindow control.
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;

                // Effectue la gestion des logs
                bool rethrow = ExceptionManager.Instance.HandleException(e.ExceptionObject, ExceptionManager.DEFAULT_POLICY);
                if (rethrow)
                {
                    ChildWindow errorWin = new ErrorWindow(e.ExceptionObject);
                    errorWin.Show();
                }
            }
        } 

        #endregion
    }
}